package com.huaxin.hxcloud.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;

import com.huaxin.hxcloud.R;

import net.qiujuer.genius.ui.widget.Button;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2016/11/9.
 */

public class DatePickerDialog extends Dialog implements NumberPicker.OnValueChangeListener {

    @BindView(R.id.date)
    TextView     date;
    @BindView(R.id.year)
    NumberPicker yearPicker;
    @BindView(R.id.month)
    NumberPicker monthPicker;
    @BindView(R.id.ok)
    Button       ok;
    Unbinder unbinder;
    private OnDateSetListener listener;

    private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");

    private Calendar mCalendar    = Calendar.getInstance();
    private Calendar tempCalendar = Calendar.getInstance();

    private Date MIN_DATE = new Date(0);
    private Date MAX_DATE = new Date();

    public DatePickerDialog(Context context) {
        super(context, R.style.DialogTheme);
    }

    public DatePickerDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public DatePickerDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public DatePickerDialog(Context context, Date date, OnDateSetListener onDateSetListener) {
        super(context, R.style.DialogTheme);
        mCalendar.setTime(date);
        listener = onDateSetListener;

    }


    public void show(Date date) {
        super.show();
        mCalendar.setTime(date);
        upDateView();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.dialog_date_picker);
        unbinder = ButterKnife.bind(this);

        tempCalendar.setTime(MIN_DATE);
        yearPicker.setMinValue(tempCalendar.get(Calendar.YEAR));
        monthPicker.setMinValue(1);
        tempCalendar.setTime(MAX_DATE);
        yearPicker.setMaxValue(tempCalendar.get(Calendar.YEAR));
        monthPicker.setMaxValue(12);
        yearPicker.setWrapSelectorWheel(false);
        yearPicker.setOnValueChangedListener(this);
        monthPicker.setOnValueChangedListener(this);
        yearPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        monthPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        upDateView();
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDateSet(mCalendar.getTime());
                }
                dismiss();
            }
        });
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

        if (picker.equals(monthPicker)) {
            if (oldVal == 1 && newVal == 12) {
                mCalendar.add(Calendar.MONTH, -1);
            } else if (oldVal == 12 && newVal == 1) {
                mCalendar.add(Calendar.MONTH, 1);
            } else {
                mCalendar.add(Calendar.MONTH, newVal - oldVal);
            }
            if (newVal == 1) {
                monthPicker.setWrapSelectorWheel(true);
            }
        } else if (picker.equals(yearPicker)) {
            mCalendar.set(Calendar.YEAR, newVal);
        }
        upDateView();

    }

    private void upDateView() {
        if (mCalendar.getTime().before(MIN_DATE)) {
            mCalendar.setTime(MIN_DATE);
        }
        if (mCalendar.getTime().after(MAX_DATE)) {
            mCalendar.setTime(MAX_DATE);
        }
        tempCalendar.setTime(MAX_DATE);
        if (mCalendar.get(Calendar.YEAR) == tempCalendar.get(Calendar.YEAR)) {
            monthPicker.setMaxValue(tempCalendar.get(Calendar.MONTH) + 1);
            monthPicker.setWrapSelectorWheel(false);
        } else {
            monthPicker.setMaxValue(12);
            monthPicker.setWrapSelectorWheel(true);
        }
        yearPicker.setValue(mCalendar.get(Calendar.YEAR));
        monthPicker.setValue(mCalendar.get(Calendar.MONTH) + 1);
        date.setText(format.format(mCalendar.getTime()));
    }

    @Override
    protected void onStop() {
        unbinder.unbind();
        super.onStop();

    }

    public interface OnDateSetListener {
        void onDateSet(Date date);
    }
}
